﻿import { AfterViewInit, Component, Injector, ViewEncapsulation, ViewChild, Output, EventEmitter } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { UserServiceProxy, UserDelegationServiceProxy, UserDelegationDto, DelegationStatus, TwoFactorLoginSettingsEditDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { AddMemberModalComponent } from '@app/admin/organization-units/add-member-modal.component';
import * as _ from 'lodash';
import { IUsersWithOrganizationUnit } from './users-with-organization-unit';
import { INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS } from '@angular/platform-browser-dynamic/src/platform_providers';

export interface IViewDelegation {
    delegationId: number;
    userId: string;
    userName: string;
    ouName: string;
    openedStatus: boolean;
}


@Component({
    templateUrl: './delegation-list.component.html',
    selector: 'delegationList'
})

export class DelegationListComponent extends AppComponentBase implements AfterViewInit {
    @ViewChild('addMemberModal') addMemberModal: AddMemberModalComponent;
    @Output() membersAdded = new EventEmitter<IUsersWithOrganizationUnit>();

    userDelegations: UserDelegationDto[];
    viewDelegations: IViewDelegation[] = [];
    blockedDelegationList = true;

    constructor(
        injector: Injector,
        private _userDelegationServiceProxy: UserDelegationServiceProxy,
        private _userServiceProxy: UserServiceProxy
    ) {
        super(injector);
    }


    openAddMemberModal(): void {
        this.addMemberModal.show();
    }

    delDelegation(delegationId: number): void {
        this._userDelegationServiceProxy
            .deleteUserDelegation(delegationId)
            .subscribe(result => {
                this.notify.success('删除成功');
                this.viewDelegations.splice(this.viewDelegations.findIndex((element) => (element.delegationId === delegationId)), 1);
            });
    }

    changeDelegationState(delegationId: number, isOpened: boolean): void {
        if (isOpened) {
                this._userDelegationServiceProxy
                .offDelegation(delegationId)
                .subscribe(result => {
                    this.notify.success('关闭成功');
                });
            } else {
                this._userDelegationServiceProxy
                .onDelegation(delegationId)
                .subscribe(result => {
                    this.notify.success('打开成功');
                });
            }
    }

    ngAfterViewInit(): void {
        this.blockedDelegationList = true;
        this._userDelegationServiceProxy.getDelegations()
            .subscribe(result => {
                this.blockedDelegationList = false;

                _.forEach(result, item => {

                    let userName = '';
                    let ouName = '';
                    let opened = false;

                    if ((Number)(item.openedStatus) === 0) {
                        opened = false;
                    } else {
                        opened = true;
                    }

                    let viewDelegation: IViewDelegation = {
                        openedStatus : opened,
                        userId : item.delegateeId,
                        userName : userName,
                        ouName : ouName,
                        delegationId : item.id
                    };
                    this.viewDelegations.push(viewDelegation);

                    this._userServiceProxy.getUserForAll(item.delegateeId)
                    .subscribe(result => {
                        let currentD = this.viewDelegations.find((element) => (element.delegationId === item.id));
                        currentD.userName =  result.user.name;
                        currentD.ouName = result.allOrganizationUnits[0].displayName;
                    });
                });

            });

    }

    addMembers(data: any): void {
        this.membersAdded.emit({
            userIds: data._userDelegationServiceProxy
        });
        this.viewDelegations = [];
        this.ngAfterViewInit();
    }
}
